local P = ClassFromFile(...)




function P.LoadAssetsAsync(t, priority, callBack)
	local totalNum = 0
	for k,v in pairs(t) do
		totalNum = totalNum + 1;
	end

	t.callBack = callBack
	t.totalNum = totalNum
	t.loadedNum = 0
	for k,v in pairs(t) do
		if type(v) == "table" then
			P.LoadAssetsAsync2(t, v, priority)
		end
	end
end

function P.LoadAssetsAsync2(assetTable, assetInfo, priority)
	assetInfo.assetTable = assetTable
	assetInfo.callback = CreateCallBack(P.LoadAssetAsyncCallback, assetInfo)
	ObjectPool.LoadAssetAsync(assetInfo.path, priority, assetInfo.callback)
end

function P.LoadAssetAsyncCallback(assetInfo, asset, obj)
	if not assetInfo.callback then
		return
	end

	assetInfo.callback = nil
	assetInfo.asset = asset
	assetInfo.obj = obj

	assetInfo.assetTable.loadedNum = assetInfo.assetTable.loadedNum + 1

	if assetInfo.assetTable.loadedNum < assetInfo.assetTable.totalNum then
		-- if assetInfo.obj and assetInfo.obj:GetType() == typeof(UnityEngine.GameObject) then
			ObjectPool.MoveToRecycledPosition(assetInfo.obj)
		-- end
	end

	
	if assetInfo.assetTable.loadedNum == assetInfo.assetTable.totalNum then
		local callback = assetInfo.assetTable.callBack
		assetInfo.assetTable.callBack = nil
		assetInfo.assetTable.totalNum = nil
		assetInfo.assetTable.loadedNum = nil
		callback()
	end
end

function P.RecycleAssets(t)
	for k,v in pairs(t) do
		if type(v) == "table" then
			if v.obj then
				ObjectPool.Recycle(v.obj)
			elseif v.callback then
				ObjectPool.CancelAsyncLoad(v.path, v.callback)
				v.callback = nil
			end
		end
	end
end